//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;
using Microsoft.TridentWordAddIn.ResourceHelper;
using System.Collections.ObjectModel;

namespace Microsoft.TridentWordAddIn.Presentation
{
    internal partial class EditAssociation : BaseForm
    {
        int documentKey;
        string associationName = string.Empty;
        IController controller;

        /// <summary>
        /// Initializes a new instance of the <see cref="EditAssociation"/> class.
        /// </summary>
        public EditAssociation()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the name of the control.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The name of the control. The default is an empty string ("").
        /// </returns>
        public string AssociationName
        {
            get
            {
                return this.ctrAnnotation.AssociationName;
            }
        }

        /// <summary>
        /// Gets the annotation.
        /// </summary>
        /// <value>The annotation.</value>
        public string Annotation
        {
            get
            {
                return this.ctrAnnotation.Annotation;
            }
        }

        /// <summary>
        /// Initializes the specified document key.
        /// </summary>
        /// <param name="documentKey">The document key.</param>
        /// <param name="controller">The controller.</param>
        /// <param name="association">The association.</param>
        public void Initialize(int documentKey, IController controller, WorkflowAssociation association)
        {
            this.documentKey = documentKey;
            this.controller = controller;

            this.lblJobId.Text = string.Format(lblJobId.Text, association.Job.Id);
            this.lblJobName.Text = string.Format(lblJobName.Text, association.Job.Name);
            this.associationName = association.Name;
            this.ctrAnnotation.AssociationName = this.associationName;
            this.ctrAnnotation.Annotation = association.Annotation;
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateSelection())
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        /// <summary>
        /// Validates the selection.
        /// </summary>
        /// <returns></returns>
        private bool ValidateSelection()
        {
            string message = string.Empty;
            if (this.ctrAnnotation.AssociationName.IsNullOrEmpty())
            {
                message = ResourceFetcher.GetString("AssociationNameBlank");
            }
            else if (this.AssociationExistsByName(this.ctrAnnotation.AssociationName))
            {
                message = string.Format(
                    ResourceFetcher.GetString("AssociationByNameAlreadyExists"),
                    this.ctrAnnotation.AssociationName);
            }
            else if (controller.IsTransactionActive(documentKey))
            {
                message = ResourceFetcher.GetString("JobExecutionInProgress");
            }

            if (message.Length != 0)
            {
                MessageBoxHelper.ShowDefault(message, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        /// <summary>
        /// Associations the name of the exists by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        private bool AssociationExistsByName(string name)
        {
            ReadOnlyCollection<WorkflowAssociation> associationList = controller.GetAllWorkflowAssociations(documentKey);
            var result = from association in associationList
                         where name == association.Name
                         where associationName != association.Name
                         select association;

            return result.ToList().Count == 0 ? false : true;
        }
    }
}
